﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace raktajino
{
    public partial class UserEulaDialog : Form
    {
        public Package installingPackage = null;

        public UserEulaDialog()
        {
            InitializeComponent();
        }

        private void UserEulaDialog_Load(object sender, EventArgs e)
        {

            string eulaInTempSubDirectory;
            Uri uri = null;
            //check if the Eula is http if so download first and then display to the user.
            try
            {
                uri = new Uri(installingPackage.EulaUrl);
            }
            catch (SystemException ex)
            {
                //this is not a nice Eula entry -> abort
                MessageBox.Show("An error occurred with the EULA:\n" + ex.Message + "\nInstallation aborted", "Error");
                this.DialogResult = DialogResult.Abort;
                return;
            }

            if (uri.IsFile) //Eula is normal file
            {
                eulaInTempSubDirectory = uri.ToString();
            }
            else    //Eula is http - download first before showing it to the user
            {
                if (!uri.IsAbsoluteUri)
                {
                    MessageBox.Show("Tool does not support relative URLs", "Error");
                    this.DialogResult = DialogResult.Abort;
                    return;
                }
                else
                {
                    //uri is valid hyperlink - comanduri is set to the downloaddirectory 
                    WebClient downloadEulaClient = new WebClient();
                    eulaInTempSubDirectory = Environment.CurrentDirectory + "\\" + System.Configuration.ConfigurationSettings.AppSettings["TempSubDirectory"] + "\\" +System.IO.Path.GetFileName(uri.LocalPath);

                    if (!Directory.Exists(Path.GetDirectoryName(eulaInTempSubDirectory)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(eulaInTempSubDirectory));
                    }
                    try
                    {
                        downloadEulaClient.DownloadFile(uri, eulaInTempSubDirectory);
                    }
                    catch (System.Net.WebException webEx)
                    {
                        //MessageBox.Show("Eula download error:\n"+webEx.Message+"\nInstallation aborted", "Error");
                        MessageBox.Show(string.Format("Eula download error:{0}\n{1}\nInstallation aborted", uri, webEx.Message), "Error");
                        this.DialogResult = DialogResult.Abort;
                        return;
                    }
                }
            }
            try
            {
                richTextBox1.LoadFile(eulaInTempSubDirectory);
            }
            catch (System.ArgumentException ex)
            {
                //Eula is not RTF or ANSI try Streamreader instead
                StreamReader sr = new StreamReader(eulaInTempSubDirectory);
                string eula = sr.ReadToEnd();
                richTextBox1.Text = eula;
            }
            catch (SystemException ex)
            {
                //something bad has happened -> abort
                MessageBox.Show(ex.Message, "Error");
                this.DialogResult = DialogResult.Abort;
                return;
            }
        }

        private void radioButton_eulaaccept_CheckedChanged(object sender, EventArgs e)
        {
            switch (radioButton_eulaaccept.Checked)
            {
                case true:
                    button_next.Enabled = true;
                    break;
                case false:
                    button_next.Enabled = false;
                    break;
            }
        }

        private void button_back_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            return;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            return;
        }

        private void button_next_Click(object sender, EventArgs e)
        {
            if (radioButton_eulaaccept.Checked == true)
            {
                this.DialogResult = DialogResult.OK;
                return;
            }
        }
    }
}
